//
//  MLTextview.m
//  QuickCure37
//
//  Created by 马文星 on 16/6/22.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "MLTextview.h"

@interface MLTextview () <UITextInput>

/** 占位符文字 Label */
@property (nonatomic, strong) UILabel *plachHolderLabel;


@end

@implementation MLTextview

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
     
        [self setupUI];
    }
    return self;
}


/** 文字 */
-(void)setPlaceHolder:(NSString *)placeHolder{
    
    if (![placeHolder isEqualToString:_placeHolder]) {
        
        _placeHolder = placeHolder;
        
        self.plachHolderLabel.text = placeHolder;
    }
}

/** 颜色 */
-(void)setPlaceHolderColor:(UIColor *)placeHolderColor{
    
    if (placeHolderColor != _placeHolderColor) {
        
        _placeHolderColor = placeHolderColor;
        
        self.plachHolderLabel.textColor = placeHolderColor;
    }
}


/** 字体 */
-(void)setPlaceHolderFont:(UIFont *)placeHolderFont{

    if (placeHolderFont != _placeHolderFont) {
        
        _placeHolderFont = placeHolderFont;
        
        self.plachHolderLabel.font = placeHolderFont;
    }
}

- (void)setupUI{
    
    //1.创建
    self.plachHolderLabel = [[UILabel alloc] init];
    
    //2.给几个默认值
    self.plachHolderLabel.textColor = LIGHT_GRAY_COLOR;
    self.plachHolderLabel.text =  @"请输反馈信息";
    self.placeHolderFont = FONT(15);
    self.font  = FONT(15);
    
    //3.添加
    [self addSubview:self.plachHolderLabel];
    
    //4.约束
    [self.plachHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(5);
        make.top.mas_equalTo(8);
        make.right.mas_equalTo(-5);
        make.bottom.mas_equalTo(self.mas_bottom);
        
    }];
    
    //5.系统监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange:) name:UITextViewTextDidChangeNotification object:nil];
}

- (void)textChange:(NSNotification *)note{

    self.plachHolderLabel.hidden  = (self.text.length > 0) ? YES : NO;
}


-(void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}





@end
